package com.darwin.darwinUI.manager.base
{
	import com.darwin.darwinBitmapEngine.core.INode;
	import com.darwin.darwinBitmapEngine.core.Node;
	import com.darwin.darwinUI.manager.ToolTipManager;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.geom.Point;

	public class TipsUtils
	{
		public function TipsUtils()
		{
		}

		
		/**
		 *	获取舞台坐标 
		 * @param item 
		 * @param tips
		 * @param direction
		 * @param stage
		 * @param offsetX
		 * @param offsetY
		 * @return 
		 * 
		 */
		public static function getStagePosition(item:*,tips:DisplayObject,direction:String,
												stage:Stage = null,offsetX:Number = 0,offsetY:Number = 0):Point
		{
			var $itemPoint:Point = item.localToGlobal(new Point(0,0));
			var $itemWidth:Number = item.width;
			var $itemHeight:Number = item.height;
			var $newPoint:Point = new Point();
			if(item is INode)
			{
				$itemPoint.offset(-$itemWidth/2,-$itemHeight/2);
			}
			
			switch(direction)
			{
				case ToolTipManager.UP:
					$newPoint.x = $itemPoint.x + ($itemWidth - tips.width)/2 + offsetX;
					$newPoint.y = $itemPoint.y - tips.height + offsetY;
					break;
				case ToolTipManager.DOWN:
					$newPoint.x = $itemPoint.x + ($itemWidth - tips.width)/2 + offsetX;
					$newPoint.y = $itemPoint.y + $itemHeight + offsetY;
					break;
				case ToolTipManager.LEFT:
					$newPoint.x = $itemPoint.x - tips.width + offsetX;
					$newPoint.y = $itemPoint.y + ($itemHeight - tips.height)/2 + offsetY;
					break;
				case ToolTipManager.RIGHT:
					$newPoint.x = $itemPoint.x + $itemWidth + offsetX;
					$newPoint.y = $itemPoint.y + ($itemHeight - tips.height)/2 + offsetY;
					break;
			}
			
			if(stage != null)
			{
				if($newPoint.x < 0)
				{
					$newPoint.x = 0;
				}else if($newPoint.x + tips.width > stage.stageWidth)
				{
					$newPoint.x = stage.stageWidth - tips.width;
				}
				
				
				if($newPoint.y < 0)
				{
					$newPoint.y = 0;
				}else if($newPoint.y + tips.height > stage.stageHeight)
				{
					$newPoint.y = stage.stageHeight - tips.height;
				}
			}
			
			
			return $newPoint;
		}
		
	}
}